classdef TrafficManager < handle
   properties
       FILE_DOWNLOAD = 1;
       WEB_BROWSING = 2;
       VOIP = 3;
       
       connections_list = [];
       topology;
   end
   
   methods
       function obj = TrafficManager(topology)
           obj.topology = topology;
       end
       
       function simulate_multiple_connections(obj)
           for source_node_id = 1:obj.topology.number_of_hap_nodes
               number_of_users_for_current_hap = TrafficManager.get_number_of_users_for_hap();
               for current_user_id = 1:number_of_users_for_current_hap
                   dest_node_id = obj.get_random_dest_node_id();
                   obj.create_traffic_for_user(source_node_id, dest_node_id);
               end
           end
       end
   
       function create_traffic_for_user(obj, source_node_id, dest_node_id)
           current_traffic_type = obj.get_current_traffic_type();
           switch current_traffic_type
               case obj.FILE_DOWNLOAD
                   obj.create_new_file_download_packets_generator(source_node_id, dest_node_id)
               case obj.WEB_BROWSING
                   obj.create_new_web_browsing_packets_generator(source_node_id, dest_node_id)
               case obj.VOIP
                   obj.create_new_voip_packets_generator(source_node_id, dest_node_id)
           end
       end
       
       function traffic_type = get_current_traffic_type(obj)
           random_precentage = floor(rand() * 100) + 1;
           params = ParamsSingleton.instance();
           
           if (random_precentage <= params.file_download_precentage)
               traffic_type = obj.FILE_DOWNLOAD;
           elseif (random_precentage <= params.file_download_precentage + params.web_browsing_precentage)
               traffic_type = obj.WEB_BROWSING;
           else
               traffic_type = obj.VOIP;
           end
       end
       
       function dest_node_id = get_random_dest_node_id(obj)
           dest_node_id = floor(rand() * obj.topology.number_of_gap_nodes) + 1 + obj.topology.number_of_hap_nodes;
       end
       
       function create_new_file_download_packets_generator(obj, source_node_id, dest_node_id)
            params = ParamsSingleton.instance();
            
            packets_generator = ...
                FileDownloadPacketsGenerator(obj.topology.nodes(source_node_id),...
                                             obj.topology.nodes(dest_node_id),...
                                             params.file_download_packet_size_in_bytes,...
                                             params.file_download_data_rate_in_Kbps);
            
            obj.connections_list = [obj.connections_list packets_generator];
       end
       
       function create_new_web_browsing_packets_generator(obj, source_node_id, dest_node_id)
            params = ParamsSingleton.instance();
            
            packets_generator = ...
                WebBrowsingPacketsGenerator(obj.topology.nodes(source_node_id),...
                                            obj.topology.nodes(dest_node_id),...
                                            params.web_browsing_packet_size_in_bytes,...
                                            params.web_browsing_data_rate_in_Kbps);
            
            obj.connections_list = [obj.connections_list packets_generator];
       end
       
       function create_new_voip_packets_generator(obj, source_node_id, dest_node_id)
            params = ParamsSingleton.instance();
            
            packets_generator = ...
                VoipPacketsGenerator(obj.topology.nodes(source_node_id),...
                                     obj.topology.nodes(dest_node_id),...
                                     params.voip_packet_size_in_bytes,...
                                     params.voip_data_rate_in_Kbps);
            
            obj.connections_list = [obj.connections_list packets_generator];
       end
       
   end
   
   methods (Static)
       function number_of_users = get_number_of_users_for_hap()
           number_of_users = floor(rand()*10) + 1;
       end
       
   end
end